package cn.easy.mortgage.service;

import cn.easy.base.bean.PageBean;
import cn.easy.base.service.BaseService;
import cn.easy.mortgage.bean.HjPGSingleQueryBean;
import cn.easy.mortgage.bean.HjPGSingleQueryResultBean;
import cn.easy.mortgage.domain.EstateEvalInfo;
import cn.easy.mortgage.repository.EstateEvalInfoDao;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Component
public class EstateEvalInfoService extends BaseService<EstateEvalInfo> {
    @Autowired
    private EstateEvalInfoDao	estateEvalInfoDao;

    public Page<EstateEvalInfo> search(Map<String, Object> searchParams, PageBean pageBean) {
        return estateEvalInfoDao.findAll(spec(searchParams), pageBean.toPageRequest(new Sort(Direction.DESC, "createdAt")));
    }

    public List<EstateEvalInfo> search(Map<String, Object> searchParams, Sort... sort) {
        return estateEvalInfoDao.findAll(spec(searchParams), sort == null || sort.length == 0  ? new Sort(Direction.DESC, "createdAt") : sort[0]);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void delete(String id) {
        estateEvalInfoDao.delete(id);
    }

    public EstateEvalInfo getOne(String id) {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        return estateEvalInfoDao.findOne(id);
    }

    public List<EstateEvalInfo> findAll(List<String> idList) {
        return Lists.newArrayList(estateEvalInfoDao.findAll(idList));
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public EstateEvalInfo save(EstateEvalInfo estateEvalInfo) {
        return estateEvalInfoDao.save(estateEvalInfo);
    }

    public Boolean evaluatedOnToday(String houseAddress, String roomSize) {
        return estateEvalInfoDao.findHouseAddressOnToday(houseAddress, roomSize) == null ? false :true;
    }

    public EstateEvalInfo findByHouseAddressAndRoomSizeOnToday(String houseAddress, String roomSize){
        return estateEvalInfoDao.findHouseAddressOnToday(houseAddress, roomSize);
    }

    public EstateEvalInfo valueOf(String regCode, HjPGSingleQueryBean hjPGSingleQueryBean, HjPGSingleQueryResultBean hjPGSingleQueryResultBean) {
        EstateEvalInfo estateEvalInfo = new EstateEvalInfo();
        estateEvalInfo.setBegin(hjPGSingleQueryBean.getBegin());
        estateEvalInfo.setBuildingCode(hjPGSingleQueryBean.getBuildingCode());
        estateEvalInfo.setCellNo(hjPGSingleQueryBean.getCellNo());
        estateEvalInfo.setEnd(hjPGSingleQueryBean.getEnd());
        estateEvalInfo.setHouseAddress(hjPGSingleQueryBean.getHouseAddress());
        estateEvalInfo.setPropertyType(hjPGSingleQueryBean.getPropertyType());
        estateEvalInfo.setRoomNo(hjPGSingleQueryBean.getRoomNo());
        estateEvalInfo.setRoomSize(hjPGSingleQueryBean.getRoomSize());
        estateEvalInfo.setHouseTotalPrice(BigDecimal.valueOf(Double.parseDouble(hjPGSingleQueryResultBean.getFv_house_total_price())));
        estateEvalInfo.setHouseUnitPrice(BigDecimal.valueOf(Double.parseDouble(hjPGSingleQueryResultBean.getFv_house_unit_price())));
        if ( hjPGSingleQueryResultBean.getFv_land_use_rights() != null) {
            estateEvalInfo.setLandUseRights(hjPGSingleQueryResultBean.getFv_land_use_rights());
        }
        estateEvalInfo.setBuildStruct(hjPGSingleQueryResultBean.getFv_build_struct());
        estateEvalInfo.setBuildYear(hjPGSingleQueryResultBean.getFv_build_year());
        estateEvalInfo.setArea(regCode);
        return estateEvalInfo;
    }
}


